// 人员管理控制器
define('managePeopleController', ['angular','jquery'], function (angular,$) {
    var module = angular.module('controllers');
    module.controller('managePeopleController', ['$rootScope','$scope','$filter','$route','userServers','companyServers',function ($rootScope,$scope,$filter,$route,userServers,companyServers) {
        // formData这个是用来搜索的
        $scope.formData={
            name:"",
            company:"",
            type:"",
            phone:"",
            company_id:""
        };
        $scope.setData={};
        // $scope.data人员列表
        $scope.submitForm=function(){
            $scope.formData = $filter('simplifyJsonFilter')($scope.formData);
            userServers.userList($scope.formData).then(function(data) {
                $scope.data=data;
            });
        };
        $scope.submitForm();
        // 查找分公司人员
        $scope.staffSearch=function(){
            userServers.userList().then(function (data) {
                $rootScope.userList=data;
                // $scope.userList=$filter("arrChoosenFilter")(angular.copy($rootScope.userList),{'company':companyid});
                // console.log($scope.userList);
                var companyid;
                if(!$scope.company){
                    $scope.userList=angular.copy($rootScope.userList);
                }else{
                    companyid=$scope.company;
                    $scope.userList=$filter("arrChoosenFilter")(angular.copy($rootScope.userList),{'company':companyid});
                }
                $scope.data=$scope.userList;
            });
        };
        $scope.staffSearch();
        // 删除人员
        $scope.del=function(v){
            if(confirm('确定删除?')===false)
                return false;
            userServers.delUser(v).then(function(data) {
                if(data.data===true){
                    $route.reload();
                }else{
                    alert('失败');
                }
            });                 
        }; 
        // 设置人员
        $scope.set=function(data){
            $scope.setData=angular.copy(data);
            console.log(data);
            $("#myModal").modal('show');
        };
        $scope.setForm=function(){
            if(confirm('确定修改?')===false)
                return false;
            delete $scope.setData.company_name;
            $scope.setData = $filter('simplifyJsonFilter')($scope.setData);
            userServers.setUser($scope.setData).then(function(data){
                if(data.data===true){
                    $route.reload();
                    $("#myModal").modal('hide');
                }else{
                    alert('失败');
                }
            });
        };
        
    }]);
});
